package vsvm.gui.forms;

import javax.swing.JDialog;
import java.awt.Frame;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Insets;

import vsvm.data.source.SVMFileDataSource;

public class ConfigureSVMDataSourceDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 100L;
	private JPanel pnlButtons = null;
	private JButton btnOk = null;
	private JButton btnCancel = null;
	private JPanel pnlCenter = null;
	private JLabel lblName = null;
	private JLabel lblFile = null;
	private JLabel lblDelim1 = null;
	private JLabel lblDelim2 = null;
	private JTextField txtName = null;
	private JTextField txtFile = null;
	private JTextField txtDelim1 = null;
	private JTextField txtDelim2 = null;
	private JButton btnBrowse = null;
	
	private SVMFileDataSource ds = null;
	private ConfigureSVMDataSourceDialog me = null;
	private JPanel mainPanel = null;
	/**
	 * This method initializes 
	 * 
	 */
	public ConfigureSVMDataSourceDialog(Frame parent, SVMFileDataSource ds) {
		super(parent);
		this.ds = ds;
		this.me = this;
		initialize();
		setLocationRelativeTo(parent);
		
		loadData();
	}

	private void loadData() {
		txtName.setText(ds.getName());
		txtDelim1.setText(ds.getDelimiter1());
		txtDelim2.setText(ds.getDelimiter2());
		txtFile.setText(ds.getFilePath());		
	}
	
	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setSize(new Dimension(408, 216));
        this.setContentPane(getMainPanel());
        this.setTitle("Configure SVM Data Source");
			
	}

	/**
	 * This method initializes pnlButtons	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlButtons() {
		if (pnlButtons == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(50);
			flowLayout.setAlignment(java.awt.FlowLayout.CENTER);
			flowLayout.setVgap(5);
			pnlButtons = new JPanel();
			pnlButtons.setLayout(flowLayout);
			pnlButtons.add(getBtnOk(), null);
			pnlButtons.add(getBtnCancel(), null);
		}
		return pnlButtons;
	}

	/**
	 * This method initializes btnOk	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton();
			btnOk.setText("   Ok    ");
			btnOk.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (txtName.getText().trim().equals("")) {
						JOptionPane.showMessageDialog(me, "Name cannot be empty!", "Error", 0);
						return;
					}					
					if (txtDelim1.getText().equals("")) {
						JOptionPane.showMessageDialog(me, "Delimiters cannot be empty!", "Error", 0);
						return;
					}
					if (txtDelim2.getText().equals("")) {
						JOptionPane.showMessageDialog(me, "Delimiters cannot be empty!", "Error", 0);
						return;
					}
					if (txtFile.getText().trim().equals("")) {
						JOptionPane.showMessageDialog(me, "File cannot be empty!", "Error", 0);
						return;
					}
					
					me.ds.setName(txtName.getText());
					me.ds.setDelimiter1(txtDelim1.getText());
					me.ds.setDelimiter2(txtDelim2.getText());
					me.ds.setFilePath(txtFile.getText());
					
					me.dispose();
				}
			});
		}
		return btnOk;
	}

	/**
	 * This method initializes btnCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setText("Cancel");
			btnCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					me.dispose();
				}
			});
		}
		return btnCancel;
	}

	/**
	 * This method initializes pnlCenter	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlCenter() {
		if (pnlCenter == null) {
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 2;
			gridBagConstraints11.insets = new Insets(10, 5, 5, 10);
			gridBagConstraints11.ipadx = 0;
			gridBagConstraints11.ipady = 0;
			gridBagConstraints11.gridwidth = 1;
			gridBagConstraints11.weightx = 0.0;
			gridBagConstraints11.weighty = 0.0;
			gridBagConstraints11.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints11.gridheight = 1;
			gridBagConstraints11.gridy = 1;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridy = 3;
			gridBagConstraints7.insets = new Insets(10, 10, 5, 20);
			gridBagConstraints7.ipadx = 100;
			gridBagConstraints7.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints7.gridx = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridy = 2;
			gridBagConstraints6.insets = new Insets(10, 10, 5, 20);
			gridBagConstraints6.ipadx = 100;
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.gridx = 1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridy = 1;
			gridBagConstraints5.insets = new Insets(10, 10, 5, 20);
			gridBagConstraints5.ipadx = 100;
			gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.insets = new Insets(10, 10, 5, 20);
			gridBagConstraints4.ipadx = 100;
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridx = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.insets = new Insets(10, 20, 5, 20);
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 3;
			lblDelim2 = new JLabel();
			lblDelim2.setText("Second delimiter");
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.insets = new Insets(10, 20, 5, 20);
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.gridy = 2;
			lblDelim1 = new JLabel();
			lblDelim1.setText("First delimiter (space)");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.insets = new Insets(10, 20, 5, 20);
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 1;
			lblFile = new JLabel();
			lblFile.setText("File");
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new Insets(10, 20, 5, 20);
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.gridy = 0;
			lblName = new JLabel();
			lblName.setText("Name");
			pnlCenter = new JPanel();
			pnlCenter.setLayout(new GridBagLayout());
			pnlCenter.add(lblName, gridBagConstraints);
			pnlCenter.add(lblFile, gridBagConstraints1);
			pnlCenter.add(lblDelim1, gridBagConstraints2);
			pnlCenter.add(lblDelim2, gridBagConstraints3);
			pnlCenter.add(getTxtName(), gridBagConstraints4);
			pnlCenter.add(getTxtFile(), gridBagConstraints5);
			pnlCenter.add(getTxtDelim1(), gridBagConstraints6);
			pnlCenter.add(getTxtDelim2(), gridBagConstraints7);
			pnlCenter.add(getBtnBrowse(), gridBagConstraints11);
		}
		return pnlCenter;
	}

	/**
	 * This method initializes txtName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtName() {
		if (txtName == null) {
			txtName = new JTextField();
		}
		return txtName;
	}

	/**
	 * This method initializes txtFile	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtFile() {
		if (txtFile == null) {
			txtFile = new JTextField();
		}
		return txtFile;
	}

	/**
	 * This method initializes txtDelim1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtDelim1() {
		if (txtDelim1 == null) {
			txtDelim1 = new JTextField();
			txtDelim1.setText(" ");
		}
		return txtDelim1;
	}

	/**
	 * This method initializes txtDelim2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtDelim2() {
		if (txtDelim2 == null) {
			txtDelim2 = new JTextField();
			txtDelim2.setText(":");
		}
		return txtDelim2;
	}

	/**
	 * This method initializes btnBrowse	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnBrowse() {
		if (btnBrowse == null) {
			btnBrowse = new JButton();
			btnBrowse.setText("...");
			btnBrowse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
					if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(ConfigureSVMDataSourceDialog.this)) {
						txtFile.setText(fc.getSelectedFile().getPath());
					}
				}
			});
		}
		return btnBrowse;
	}

	/**
	 * This method initializes mainPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getMainPanel() {
		if (mainPanel == null) {
			mainPanel = new JPanel();
			mainPanel.setLayout(new BorderLayout());
			mainPanel.add(getPnlButtons(), BorderLayout.SOUTH);
			mainPanel.add(getPnlCenter(), BorderLayout.CENTER);
		}
		return mainPanel;
	}


}  //  @jve:decl-index=0:visual-constraint="96,13"
